package service

import (
	"gorm.io/gorm"
	"guns-go/global"
	"guns-go/model"
	"strconv"
)

const (
	ExtOrgCodeDictKey = "orgCode"
	ExtOrgNameDictKey = "orgName"
	ExtPosCodeDictKey = "posCode"
	ExtPosNameDictKey = "posName"
	ExtOrgIdDictKey   = "orgId"
	ExtPosIdDictKey   = "posId"
)

func GetEmpExtOrgPosDictSlice(empId int, isFillId bool) (err error, dictS []map[string]interface{}) {

	err, empExtSlice := GetEmpExtOrgPosByEmpId(empId)

	if err != nil {
		return err, nil
	}

	dictSlice := make([]map[string]interface{}, 0)

	if len(empExtSlice) > 0 {

		for _, empExt := range empExtSlice {

			dict := make(map[string]interface{}, 0)

			orgId := empExt.OrgId
			errOrgInfo, sysOrgInfo := QuerySysOrgById(orgId)

			if errOrgInfo != nil {
				return errOrgInfo, nil
			}

			dict[ExtOrgCodeDictKey] = sysOrgInfo.Code
			dict[ExtOrgNameDictKey] = sysOrgInfo.Name

			posId := empExt.PosId
			errPosInfo, sysPosInfo := QuerySysPosById(posId)

			if errPosInfo != nil {
				return errPosInfo, nil
			}

			dict[ExtPosCodeDictKey] = sysPosInfo.Code
			dict[ExtPosNameDictKey] = sysPosInfo.Name

			if isFillId {
				dict[ExtOrgIdDictKey] = strconv.Itoa(orgId)
				dict[ExtPosIdDictKey] = strconv.Itoa(posId)
			}

			dictSlice = append(dictSlice, dict)
		}
	}

	return err, dictSlice
}

func GetEmpExtOrgPosByEmpId(empId int) (err error, empExtOrgPos []model.SysEmpExtOrgPos) {

	var empExt []model.SysEmpExtOrgPos
	err = global.GVA_DB.Where("emp_id = ?", empId).Find(&empExt).Error
	return err, empExt
}

func DeleteEmpExtInfoByUserId(empId int, tx *gorm.DB) error {

	err := tx.Delete(model.SysEmpExtOrgPos{}, "emp_id = ?", empId).Error
	return err
}

func DeleteEmpExtInfoById(empId int, tx *gorm.DB) error {

	err := tx.Delete(model.SysEmpExtOrgPos{}, "id = ?", empId).Error
	return err
}

func ExtOrgPosAddOrEdit(empId int, extIdSlice []map[string]interface{}, tx *gorm.DB) error {

	err, sysEmpExtOrgPos := GetEmpExtOrgPosByEmpId(empId)

	if err != nil {
		return err
	}

	if len(sysEmpExtOrgPos) > 0 {
		for _, extOrgPos := range sysEmpExtOrgPos {
			//删除附属信息
			err = DeleteEmpExtInfoById(extOrgPos.Id, tx)

			if err != nil {
				return err
			}
		}
	}

	//保存附属信息
	for _, dict := range extIdSlice {

		orgId := dict[ExtOrgIdDictKey]
		posId := dict[ExtPosIdDictKey]

		sysEmpExtOrgPos := new(model.SysEmpExtOrgPos)

		sysEmpExtOrgPos.EmpId = empId
		orgIdInt, _ := strconv.Atoi(orgId.(string))
		posIdInt, _ := strconv.Atoi(posId.(string))
		sysEmpExtOrgPos.OrgId = orgIdInt
		sysEmpExtOrgPos.PosId = posIdInt

		err = ExtOrgPosSave(sysEmpExtOrgPos, tx)

		if err != nil {
			return err
		}
	}
	return nil
}

func ExtOrgPosSave(extOrgPos *model.SysEmpExtOrgPos, tx *gorm.DB) error {
	return tx.Create(extOrgPos).Error
}

func HasExtOrgEmp(orgId int) (error, bool) {

	var sysEmpExtOrgPosSlice []*model.SysEmpExtOrgPos

	err := global.GVA_DB.Where("org_id", orgId).Find(&sysEmpExtOrgPosSlice).Error

	if err != nil {
		return err, false
	}
	return nil, len(sysEmpExtOrgPosSlice) != 0
}

func HasExtPosEmp(posId int) (error, bool) {

	var sysEmpExtPosSlice []*model.SysEmpExtOrgPos
	err := global.GVA_DB.Where("pos_id", posId).Find(&sysEmpExtPosSlice).Error

	if err != nil {
		return err, false
	}
	return nil, len(sysEmpExtPosSlice) != 0
}
